#include "DBWrapper.h"


CDBWrapper::CDBWrapper(void)
{
	wxSQLite3Database::InitializeSQLite();
	pDatabase = new wxSQLite3Database();
}

CDBWrapper::~CDBWrapper(void)
{
}

void CDBWrapper::DBExist()
{
/*
	wxString StdDataDir = wxStandardPaths::Get().GetDataDir();
	wxString DBName = StdDataDir + wxT("\\FusionSpy.db"); 

	if (! wxFileExists(DBName))
	{
		wxMessageBox("Can't Find FusionSpy.db, Will Create a new one!!");
		DBInit( DBName );
	}
*/
}
int CDBWrapper::DBInit(wxString dbfile, int flag )
{
#if 1
	//System Table in User Database.
	wxString SysRecListDDL = wxT("Create  TABLE  sysreclist (\
					[RecTime] datetime NOT NULL\
					,[ProductVer] varchar\
					,[SystemBoot] varchar\
					,[OSType] varchar\
					,[AgentVer] varchar\
					);");

	//Tuxedo Table in User Database
	wxString TuxResourceDDL = wxT("Create  TABLE  tuxresource (\
					[RecTime] datetime NOT NULL\
					,[TuxRunSvr] int\
					,[TuxRunQue] int\
					,[TuxRunClt] int\
					,[CPUIdle] int\
					,[MemFree] float\
					);");

	wxString TuxSvrListDDL = wxT("Create  TABLE  tuxsvrlist (\
					[ProgName] varchar NOT NULL\
					,[MemoryUse] varchar\
					,[CurrentSVC] varchar\
					,[SvrMIN] int\
					,[SvrMAX] int\
					,[RqDone] varchar\
					,[ProcessID] varchar\
					,[QueueName] varchar\
					,[RecTime] datetime\
					);");

	wxString TuxQueListDDL = wxT("Create  TABLE tuxquelist(\
					[ProgName] varchar\
					,[Queued] int\
					,[SvrCnt] int\
					,[IPCSID] varchar\
					,[RecTime] datetime\
					);");
	
	wxString TuxCltListDDL = wxT("Create  TABLE tuxcltlist(\
					[ConnTime] varchar\
					,[ClientStatus] varchar\
					,[ClientAddr] varchar\
					,[ClientPID] varchar\
					,[ClientName] varchar\
					,[RecTime] datetime\
					);");

	wxString TuxAlarmListDDL = wxT("Create  TABLE tuxalarmlist(\
					[SiteName] varchar UNIQUE\
					,[SvrDied] varchar\
					,[LongBusy] varchar\
					,[NoTran] varchar\
					,[Queued] varchar\
					,[SMS] varchar\
					,[Email] varchar\
					,[PlaySound] varchar\
					);");
	
	wxString WlsResourceDDL = wxT("Create  TABLE wlsresource (\
					[RecTime] datetime NOT NULL\
					,[WlsSvrNum] int\
					,[WlsRunSvr] int\
					,[CPUIdle] int\
					,[MemFree] float\
					);");


	wxString WlsSvrListDDL = wxT("CREATE TABLE wlssvrlist (\
					[ServerName] varchar NOT NULL\
					,[URL] varchar\
					,[Port] int\
					,[Health] varchar\
					,[State] varchar\
					,[RecTime] datetime\
					);");

	wxString WlsJvmListDDL = wxT("CREATE TABLE wlsjvmlist (\
					[ServerName] varchar NOT NULL\
					,[JVMVendor] varchar\
					,[JVMVersion] varchar\
					,[FreeHeap] varchar\
					,[CurrentHeap] varchar\
					,[HeapSizeMax] varchar\
					,[FreePercent] varchar\
					,[RecTime] datetime\
					);");

	wxString WlsThdListDDL = wxT("CREATE TABLE wlsthdlist (\
					[ServerName] varchar NOT NULL\
					,[TotalCount] int\
					,[IdleCount] int\
					,[Throughput] int\
					,[QueueLength] int\
					,[RecTime] datetime\
					);");

	wxString WlsJDBCListDDL = wxT("CREATE TABLE wlsjdbclist (\
					[ServerName] varchar NOT NULL\
					,[JDBCName] varchar\
					,[ActiveCurrent] int\
					,[ActiveHigh] int\
					,[CurrCapacity] int \
					,[LeakCount] int\
					,[State] varchar\
					,[RecTime] datetime\
					);");

	wxString WlsAlarmListDDL = wxT("Create TABLE wlsalarmlist(\
					[SiteName] varchar UNIQUE\
					,[SvrDied] varchar\
					,[FreeHeap] varchar\
					,[IdleThread] varchar\
					,[IdleJDBC] varchar\
					,[SMS] varchar\
					,[Email] varchar\
					,[PlaySound] varchar\
					,[ThreadDump] varchar\
					);");


	//Admin Database Table
	wxString SiteListDDL = wxT("Create  TABLE sitelist(\
					[SiteName] varchar UNIQUE\
					,[SiteType] varchar\
					,[SiteIP] varchar\
					,[SitePort] int\
					,[SiteAuth] varchar\
					,[SitePwd] varchar\
					,[WlsAdminURL] varchar\
					,[WlsVer] varchar\
					,[SiteFlag] varchar\
					,[DatabaseFile] varchar\
					);");

	wxString ActionSetDDL = wxT("Create  TABLE actionset(\
					[SMSTo] varchar\
					,[MailTo] varchar\
					,[SMTPPwd] varchar\
					,[SMTPUsr] varchar\
					,[SMTPSvr] varchar\
					,[SoundFile] varchar\
					);");
#endif
	if (wxFileExists(dbfile))
	{
		wxRemoveFile(dbfile);
	}

	wxSQLite3Database* db = new wxSQLite3Database();
	db->Open( dbfile );

	if( flag == ADMINTABLE ) {
		db->ExecuteUpdate( SiteListDDL );
		db->ExecuteUpdate( ActionSetDDL );
	} else if( flag == TUXTABLE)  {
		db->ExecuteUpdate( SysRecListDDL );
		db->ExecuteUpdate( TuxResourceDDL );
		db->ExecuteUpdate( TuxAlarmListDDL );
		db->ExecuteUpdate( TuxQueListDDL );
		db->ExecuteUpdate( TuxCltListDDL );
		db->ExecuteUpdate( TuxSvrListDDL );
	} else if( flag == WLSTABLE)  {
		db->ExecuteUpdate( SysRecListDDL );
		db->ExecuteUpdate( WlsResourceDDL );
		db->ExecuteUpdate( WlsAlarmListDDL );
		db->ExecuteUpdate( WlsSvrListDDL );
		db->ExecuteUpdate( WlsJvmListDDL );
		db->ExecuteUpdate( WlsThdListDDL );
		db->ExecuteUpdate( WlsJDBCListDDL );
	}
	
	db->Close( );
	delete db;

	return 1;
}

int CDBWrapper::DBOpen(wxString dbfile, int flag)
{
	if (! wxFileExists(dbfile))
	{
		wxString errmsg = wxString::Format(wxT("Can't find database [%s]!, will create a new one!"), dbfile.mb_str()); 
		wxMessageBox( errmsg );
		DBInit( dbfile, flag );
	}

	pDatabase->Open(dbfile);

	m_dbfile = dbfile;

	return 1;
}

int CDBWrapper::WlsSvrLstSave( WebLogicServerArray wlssvrarray, wxString currenttime )
{
	wxString SqlClause, SqlTemplate;
	wxChar Value[1024];
	WebLogicServer wlssvr;
	int nRows = 0;

	SqlTemplate = 
			wxT("insert into wlssvrlist(RecTime, ServerName, URL, Port, Health, State ) ");


	for( int i = 0,j = 0; i < (wxInt16)wlssvrarray.GetCount() ; i ++ ) {
		wlssvr = wlssvrarray.Item(i);
		memset( Value, 0, sizeof(Value) );
		sprintf(Value, wxT("values ('%s', '%s','%s','%s','%s', '%s');"), currenttime.mb_str(),
				wlssvr.ServerName.mb_str() , wlssvr.URL.mb_str(), wlssvr.PORT.mb_str(), wlssvr.HealthState.mb_str(),
				wlssvr.State.mb_str() );

		SqlClause = SqlTemplate + Value;

		nRows = pDatabase->ExecuteUpdate( SqlClause );
	}

	return nRows;
}

int CDBWrapper::WlsJvmLstSave( WebLogicJVMArray wlsjvmarray, wxString currenttime )
{
	wxString SqlClause, SqlTemplate;
	wxChar Value[1024];
	WebLogicJVM wlsjvm;
	int nRows = 0;

	SqlTemplate = 
			wxT("insert into wlsjvmlist(RecTime, ServerName, JVMVendor, JVMVersion, FreeHeap, CurrentHeap, HeapSizeMax,FreePercent ) ");

	for( int i = 0,j = 0; i < (wxInt16)wlsjvmarray.GetCount() ; i ++ ) {
		wlsjvm = wlsjvmarray.Item(i);
		memset( Value, 0, sizeof(Value) );
		sprintf(Value, wxT("values ('%s', '%s','%s','%s','%s', '%s', '%s', '%s');"), currenttime.mb_str(),
				wlsjvm.ServerName.mb_str() , wlsjvm.Vendor.mb_str(), wlsjvm.Version.mb_str(), wlsjvm.FreeHeap.mb_str(),
				wlsjvm.CurrHeap.mb_str(), wlsjvm.HeapSizeMax.mb_str(), wlsjvm.FreePercent.mb_str() );

		SqlClause = SqlTemplate + Value;

		nRows = pDatabase->ExecuteUpdate( SqlClause );
	}

	return nRows;

}

int CDBWrapper::WlsThdLstSave( WebLogicThreadArray wlsthdarray, wxString currenttime )
{
	wxString SqlClause, SqlTemplate;
	wxChar Value[1024];
	WebLogicThread wlsthd;
	int nRows = 0;

	SqlTemplate = 
			wxT("insert into wlsthdlist(RecTime, ServerName, TotalCount, IdleCount, Throughput, QueueLength ) ");

	for( int i = 0,j = 0; i < (wxInt16)wlsthdarray.GetCount() ; i ++ ) {
		wlsthd = wlsthdarray.Item(i);
		memset( Value, 0, sizeof(Value) );
		sprintf(Value, wxT("values ('%s', '%s','%s','%s','%s', '%s');"), currenttime.mb_str(),
				wlsthd.ServerName.mb_str() , wlsthd.TotalCount.mb_str(), wlsthd.IdleCount.mb_str(), wlsthd.ThroughPut.mb_str(),
				wlsthd.QueueLength.mb_str() );

		SqlClause = SqlTemplate + Value;

		nRows = pDatabase->ExecuteUpdate( SqlClause );
	}

	return nRows;

}

int CDBWrapper::WlsJDBCLstSave( WebLogicJDBCArray wlsjdbcarray, wxString currenttime )
{
	wxString SqlClause, SqlTemplate;
	wxChar Value[1024];
	WebLogicJDBC wlsjdbc;
	int nRows = 0;


	SqlTemplate = 
			wxT("insert into wlsjdbclist(RecTime, ServerName, JDBCName, ActiveCurrent, ActiveHigh, CurrCapacity, LeakCount, State ) ");

	for( int i = 0,j = 0; i < (wxInt16)wlsjdbcarray.GetCount() ; i ++ ) {
		wlsjdbc = wlsjdbcarray.Item(i);
		memset( Value, 0, sizeof(Value) );
		sprintf(Value, wxT("values ('%s', '%s','%s','%s','%s', '%s', '%s', '%s');"), currenttime.mb_str(),
				wlsjdbc.ServerName.mb_str() , wlsjdbc.Name.mb_str(), wlsjdbc.ActiveConnectionsCurrentCount.mb_str(), 
				wlsjdbc.ActiveConnectionsHighCount.mb_str(), wlsjdbc.CurrCapacity.mb_str(),
				wlsjdbc.LeakedConnectionCount.mb_str(), wlsjdbc.State.mb_str() );

		SqlClause = SqlTemplate + Value;

		nRows = pDatabase->ExecuteUpdate( SqlClause );
	}

	return nRows;

}

int CDBWrapper::TuxSvrLstSave( TuxedoServerArray tuxsvrarray, wxString currenttime )
{
	wxString SqlClause, SqlTemplate;
	wxChar Value[1024];
	TuxedoServer tuxsvr;
	int nRows = 0;

	SqlTemplate = 
			wxT("insert into tuxsvrlist(RecTime, ProgName, QueueName,ProcessID,RqDone,SvrMIN, SvrMAX,CurrentSVC,MemoryUse ) ");


	for( int i = 0,j = 0; i < (wxInt16)tuxsvrarray.GetCount() ; i ++ ) {
		tuxsvr = tuxsvrarray.Item(i);
		memset( Value, 0, sizeof(Value) );
		sprintf(Value, wxT("values ('%s', '%s','%s','%s','%s', '%s', '%s', '%s', '%s');"), currenttime.mb_str(),
				tuxsvr.ProgName.mb_str() , tuxsvr.QueName.mb_str(), tuxsvr.ProcessID.mb_str(), tuxsvr.RqDone.mb_str(),
				tuxsvr.SvrMin.mb_str(), tuxsvr.SvrMax.mb_str(), tuxsvr.CurrService.mb_str(), tuxsvr.UseMem.mb_str() );

		SqlClause = SqlTemplate + Value;
#ifdef DEBUG
		wxLogMessage( SqlClause );
#endif
		nRows = pDatabase->ExecuteUpdate( SqlClause );
	}

	return nRows;

}

int CDBWrapper::TuxCltLstSave( TuxedoClientArray tuxcltarray, wxString currenttime )
{
	wxString SqlClause, SqlTemplate;
	wxChar Value[1024];
	TuxedoClient tuxclt;
	int nRows = 0;

	SqlTemplate = 
			wxT("insert into tuxcltlist(RecTime, ClientName, ClientPID,ClientAddr,ClientStatus,ConnTime ) ");

	for( int i = 0,j = 0; i < (wxInt16)tuxcltarray.GetCount() ; i ++ ) {
		tuxclt = tuxcltarray.Item(i);

		memset( Value, 0, sizeof(Value) );
		sprintf(Value, wxT("values ('%s', '%s','%s','%s','%s', '%s');"), currenttime.mb_str(),
			tuxclt.ClientName.mb_str() , tuxclt.ClientPID.mb_str(), tuxclt.ClientAddr.mb_str(),
			tuxclt.Status.mb_str(),	tuxclt.ConnectTime.mb_str() );

		SqlClause = SqlTemplate + Value;

#ifdef DEBUG
		wxLogMessage( SqlClause );
#endif

		nRows = pDatabase->ExecuteUpdate( SqlClause );
	}

	return nRows;

}

int CDBWrapper::TuxQueLstSave( TuxedoQueueArray tuxquearray, wxString currenttime )
{
	wxString SqlClause, SqlTemplate;
	wxChar Value[1024];
	TuxedoQueue tuxque;
	int nRows = 0;

	SqlTemplate = 
			wxT("insert into tuxquelist(RecTime, ProgName, SvrCnt,Queued, IPCSID ) ");

	for( int i = 0,j = 0; i < (wxInt16)tuxquearray.GetCount() ; i ++ ) {
		tuxque = tuxquearray.Item(i);

		memset( Value, 0, sizeof(Value) );
		sprintf(Value, wxT("values ('%s', '%s','%s','%s','%s');"), currenttime.mb_str(),
			tuxque.ProgName.mb_str() , tuxque.SvrCnt.mb_str(), tuxque.Queued.mb_str(), tuxque.QueName.mb_str() );

		SqlClause = SqlTemplate + Value;
#ifdef DEBUG
		wxLogMessage( SqlClause );
#endif
		nRows = pDatabase->ExecuteUpdate( SqlClause );
	}

	return nRows;

}

SiteArray CDBWrapper::getAllSite()
{
	return sitearray;
}


int CDBWrapper::SiteListQuery()
{
	Site site;

	if( !pDatabase->TableExists(wxT("SiteList")) ) {
		wxMessageBox("The Database file is incorrect!");
		return -1;
	}
	wxSQLite3ResultSet q1 = pDatabase->ExecuteQuery(wxT("select * from SiteList;"));

	sitearray.Empty();
    while (q1.NextRow())
    {
		site.SiteName = q1.GetAsString(0);
		site.SiteType = q1.GetAsString(1);
		site.SiteIP = q1.GetAsString(2);
		site.SitePort = q1.GetAsString(3);
		site.SiteAuth = q1.GetAsString(4);
		site.SitePwd = q1.GetAsString(5);
		site.WlsAdminURL = q1.GetAsString(6);
		site.WlsVer = q1.GetAsString(7);
		site.SiteFlag = q1.GetAsString(8);
		site.DatabaseFile = q1.GetAsString(9);
		sitearray.Add(site);
		//cout << (const char*)(q1.GetString(0).mb_str()) << endl;
    }

	return 1;
}

int CDBWrapper::SiteDelete( Site site )
{
	wxString SqlClause;
	wxChar Value[1024];
	int nRows = 0;

	SqlClause = 
			wxT("delete from SiteList where SiteName =  ");

	memset( Value, 0, sizeof(Value) );
	sprintf(Value, wxT("'%s';"), site.SiteName.mb_str() );

	SqlClause = SqlClause + Value;

	nRows = pDatabase->ExecuteUpdate( SqlClause );

//#if 0
//	if( flag == 1 ) {
		if (wxFileExists(site.DatabaseFile)){
			wxRemoveFile(site.DatabaseFile);
		}
//	}
//#endif

	return nRows;


}
int CDBWrapper::SiteInsert( Site site )
{
	wxString SqlClause;
	wxChar Value[1024];
	int port = 0, nRows = 0;

	site.SitePort.ToULong((unsigned long*)&port);

	SqlClause = 
			wxT("insert into SiteList(SiteName, SiteType, SiteIP, SitePort, SiteAuth, SitePwd, WlsAdminURL, WlsVer, SiteFlag, DatabaseFile ) ");

	memset( Value, 0, sizeof(Value) );
	sprintf(Value, wxT("values ('%s', '%s','%s','%d','%s','%s','%s', '%s', '%s', '%s');"), site.SiteName.mb_str(),
		site.SiteType.mb_str() , site.SiteIP.mb_str(), port, site.SiteAuth.mb_str(), site.SitePwd.mb_str(),
		site.WlsAdminURL.mb_str(), site.WlsVer.mb_str(), site.SiteFlag.mb_str(), site.DatabaseFile.mb_str() );

	SqlClause = SqlClause + Value;

	nRows = pDatabase->ExecuteUpdate( SqlClause );

	return nRows;

}

int CDBWrapper::SysRecListInsert( SysRecList sysreclist )
{
	wxString SqlClause;
	wxChar Value[1024];
	int nRows = 0;

	SqlClause = wxT("insert into sysreclist(RecTime, ProductVer, SystemBoot, OSType, AgentVer ) ");

	memset( Value, 0, sizeof(Value) );
	sprintf(Value, wxT("values ('%s', '%s','%s','%s','%s');"), 
		sysreclist.RecTime.mb_str(),sysreclist.ProductVer.mb_str() , 
		sysreclist.SystemBoot.mb_str(), sysreclist.OSType.mb_str(), sysreclist.AgentVer.mb_str() );

	SqlClause = SqlClause + Value;

	nRows = pDatabase->ExecuteUpdate( SqlClause );

	return nRows;

}

int CDBWrapper::TuxResourceInsert( TuxResource tuxresource )
{
	wxString SqlClause;
	wxChar Value[1024];
	int nRows = 0;

	SqlClause = wxT("insert into tuxresource(RecTime, TuxRunSvr, TuxRunQue, TuxRunClt, CPUIdle, MemFree ) ");

	memset( Value, 0, sizeof(Value) );
	sprintf(Value, wxT("values ('%s', '%s','%s','%s','%s', '%s');"), 
		tuxresource.sysresource.RecTime.mb_str(),tuxresource.TuxRunSvr.mb_str() , 
		tuxresource.TuxRunQue.mb_str(), tuxresource.TuxRunClt.mb_str(), tuxresource.sysresource.CPUIdle.mb_str()
		,tuxresource.sysresource.MemFree.mb_str());

	SqlClause = SqlClause + Value;

	nRows = pDatabase->ExecuteUpdate( SqlClause );

	return nRows;

}
int CDBWrapper::TuxResListQuery( wxString sqlcondition, TuxResourceArray* tuxresarray )
{
	TuxResource tuxres;
	wxInt32 nCount = 0;
	wxString sqlClause = wxT("select * from TuxResource ");

	sqlClause += sqlcondition;

	wxSQLite3ResultSet q1 = pDatabase->ExecuteQuery( sqlClause );

    while (q1.NextRow())
    {
		tuxres.sysresource.RecTime = q1.GetAsString(0);
		tuxres.TuxRunSvr = q1.GetAsString(1);
		tuxres.TuxRunQue = q1.GetAsString(2);
		tuxres.TuxRunClt = q1.GetAsString(3);
		tuxres.sysresource.CPUIdle = q1.GetAsString(4);
		tuxres.sysresource.MemFree = q1.GetAsString(5);
		tuxresarray->Add(tuxres);
		nCount ++;
		//cout << (const char*)(q1.GetString(0).mb_str()) << endl;
    }

	return nCount;
}

int CDBWrapper::WlsResourceInsert( WlsResource wlsresource )
{
	wxString SqlClause;
	wxChar Value[1024];
	int nRows = 0;

	SqlClause = wxT("insert into wlsresource(RecTime, WlsSvrNum, WlsRunSvr, CPUIdle, MemFree ) ");

	memset( Value, 0, sizeof(Value) );
	sprintf(Value, wxT("values ('%s', '%s','%s','%s', '%s');"), 
		wlsresource.sysresource.RecTime.mb_str(),wlsresource.WlsSvrNum.mb_str() , 
		wlsresource.WlsRunSvr.mb_str(), wlsresource.sysresource.CPUIdle.mb_str()
		,wlsresource.sysresource.MemFree.mb_str());

	SqlClause = SqlClause + Value;

	nRows = pDatabase->ExecuteUpdate( SqlClause );

	return nRows;

}
int CDBWrapper::WlsResListQuery( wxString sqlcondition, WlsResourceArray* wlsresarray )
{
	WlsResource wlsres;
	wxInt32 nCount = 0;
	wxString sqlClause = wxT("select * from WlsResource ");

	sqlClause += sqlcondition;

	wxSQLite3ResultSet q1 = pDatabase->ExecuteQuery( sqlClause );

    while (q1.NextRow())
    {
		wlsres.sysresource.RecTime = q1.GetAsString(0);
		wlsres.WlsSvrNum = q1.GetAsString(1);
		wlsres.WlsRunSvr = q1.GetAsString(2);
		wlsres.sysresource.CPUIdle = q1.GetAsString(3);
		wlsres.sysresource.MemFree = q1.GetAsString(4);
		wlsresarray->Add(wlsres);
		nCount ++;
    }

	return nCount;
}
TuxAlarm CDBWrapper::getTuxAlarm()
{
	return tuxalarm;
}
WlsAlarm CDBWrapper::getWlsAlarm()
{
	return wlsalarm;
}
int CDBWrapper::TuxAlarmDelete( wxString sitename )
{
	wxString SqlClause;
	wxChar Value[1024];
	int nRows = 0;

	//First. Delete 
	SqlClause = wxT("delete from TuxAlarmList where SiteName =  ");

	memset( Value, 0, sizeof(Value) );
	sprintf(Value, wxT("'%s';"), sitename.mb_str() );

	SqlClause = SqlClause + Value;

	nRows = pDatabase->ExecuteUpdate( SqlClause );
	
	return nRows;

}
int CDBWrapper::WlsAlarmDelete( wxString sitename )
{
	wxString SqlClause;
	wxChar Value[1024];
	int nRows = 0;

	//First. Delete 
	SqlClause = wxT("delete from WlsAlarmList where SiteName =  ");

	memset( Value, 0, sizeof(Value) );
	sprintf(Value, wxT("'%s';"), sitename.mb_str() );

	SqlClause = SqlClause + Value;

	nRows = pDatabase->ExecuteUpdate( SqlClause );

	return nRows;

}
int CDBWrapper::TuxAlarmUpdate( TuxAlarm tuxalarm ) 
{
	wxString SqlClause;
	wxChar Value[1024];
	int nRows = 0;

	//First. Delete 
	SqlClause = wxT("delete from TuxAlarmList where SiteName =  ");

	memset( Value, 0, sizeof(Value) );
	sprintf(Value, wxT("'%s';"), tuxalarm.SiteName.mb_str() );

	SqlClause = SqlClause + Value;

	nRows = pDatabase->ExecuteUpdate( SqlClause );

	//Second Inserts
	SqlClause = wxT("insert into TuxAlarmList(SiteName, SvrDied, LongBusy, NoTran, Queued, SMS, EMail, PlaySound )");

	memset( Value, 0, sizeof(Value) );

	sprintf(Value, wxT("values ('%s', '%s','%s','%s','%s', '%s', '%s', '%s');"), tuxalarm.SiteName.mb_str(),
		tuxalarm.SvrDied.mb_str() , tuxalarm.LongBusy.mb_str(), tuxalarm.NoTran.mb_str(), tuxalarm.Queued.mb_str(),
		tuxalarm.SMS.mb_str(), tuxalarm.Email.mb_str(),tuxalarm.PlaySound.mb_str() );
	SqlClause = SqlClause + Value;

	nRows = pDatabase->ExecuteUpdate( SqlClause );

	return nRows;

}
int CDBWrapper::WlsAlarmUpdate( WlsAlarm wlsalarm ) 
{

	wxString SqlClause;
	wxChar Value[1024];
	int nRows = 0;

	//First. Delete 
	SqlClause = wxT("delete from WlsAlarmList where SiteName =  ");

	memset( Value, 0, sizeof(Value) );
	sprintf(Value, wxT("'%s';"), wlsalarm.SiteName.mb_str() );

	SqlClause = SqlClause + Value;

	nRows = pDatabase->ExecuteUpdate( SqlClause );

	//Second Inserts
	SqlClause = wxT("insert into WlsAlarmList(SiteName, SvrDied, FreeHeap, IdleThread, IdleJDBC, SMS, EMail, PlaySound, ThreadDump )");

	memset( Value, 0, sizeof(Value) );

	sprintf(Value, wxT("values ('%s', '%s','%s','%s','%s', '%s', '%s', '%s', '%s');"), wlsalarm.SiteName.mb_str(),
		wlsalarm.SvrDied.mb_str() , wlsalarm.FreeHeap.mb_str(), wlsalarm.IdleThread.mb_str(), wlsalarm.IdleJDBC.mb_str(),
		wlsalarm.SMS.mb_str(), wlsalarm.Email.mb_str(),wlsalarm.PlaySound.mb_str(),wlsalarm.ThreadDump.mb_str() );
	SqlClause = SqlClause + Value;

	nRows = pDatabase->ExecuteUpdate( SqlClause );

	return nRows;
}
int CDBWrapper::TuxAlarmListQuery( wxString sitename)
{ //TODO
	int count = 0;

	wxString sqlclause = wxT("select * from TuxAlarmList where sitename = '");

	if( !pDatabase->TableExists(wxT("TuxAlarmList")) ) {
		wxMessageBox("The FusionSpy.db is incorrect!");
		return -1;
	}

	sqlclause += sitename;
	sqlclause += wxT("';");

	wxSQLite3ResultSet q1 = pDatabase->ExecuteQuery(sqlclause);

    while (q1.NextRow())
    {
		tuxalarm.SiteName = q1.GetAsString(0);
		tuxalarm.SvrDied = q1.GetAsString(1);
		tuxalarm.LongBusy = q1.GetAsString(2);
		tuxalarm.NoTran = q1.GetAsString(3);
		tuxalarm.Queued = q1.GetAsString(4);
		tuxalarm.SMS = q1.GetAsString(5);
		tuxalarm.Email = q1.GetAsString(6);
		tuxalarm.PlaySound = q1.GetAsString(7);
		count ++;
    }

	return count;

}
int CDBWrapper::WlsAlarmListQuery( wxString sitename)
{ 
	int count = 0;

	wxString sqlclause = wxT("select * from WlsAlarmList where sitename = '");

	if( !pDatabase->TableExists(wxT("WlsAlarmList")) ) {
		wxMessageBox("here The TuxSpy.db is incorrect!");
		return -1;
	}
	sqlclause += sitename;
	sqlclause += wxT("';");

	wxSQLite3ResultSet q1 = pDatabase->ExecuteQuery(sqlclause);

    while (q1.NextRow())
    {
		wlsalarm.SiteName = q1.GetAsString(0);
		wlsalarm.SvrDied = q1.GetAsString(1);
		wlsalarm.FreeHeap = q1.GetAsString(2);
		wlsalarm.IdleThread = q1.GetAsString(3);
		wlsalarm.IdleJDBC = q1.GetAsString(4);
		wlsalarm.SMS = q1.GetAsString(5);
		wlsalarm.Email = q1.GetAsString(6);
		wlsalarm.PlaySound = q1.GetAsString(7);
		wlsalarm.ThreadDump = q1.GetAsString(8);
		count ++;
    }

	return count;

}
int CDBWrapper::ActionSetUpdate( ActionSet actionset ) 
{
	wxString SqlClause;
	wxChar Value[1024];
	int nRows = 0;

	//First. Delete 
	SqlClause = wxT("delete from ActionSet;");

	nRows = pDatabase->ExecuteUpdate( SqlClause );

	//Second Inserts
	SqlClause = wxT("insert into ActionSet(SoundFile, SMTPSvr, SMTPUsr, SMTPPwd, MailTo, SMSTo)");

	memset( Value, 0, sizeof(Value) );

	sprintf(Value, wxT("values ('%s', '%s','%s','%s','%s', '%s');"), actionset.SoundFile.mb_str(),
		actionset.SMTPSvr.mb_str() , actionset.SMTPUsr.mb_str(), actionset.SMTPPwd.mb_str(), actionset.MailTo.mb_str(),
		actionset.SMSTo.mb_str() );

	SqlClause = SqlClause + Value;

	nRows = pDatabase->ExecuteUpdate( SqlClause );

	return nRows;

}

ActionSet CDBWrapper::getActionSet()
{
	return actionset;
}

int CDBWrapper::ActionSetQuery( )
{ 
	int count = 0;

	wxString sqlclause = wxT("select * from ActionSet;");

	wxSQLite3ResultSet q1 = pDatabase->ExecuteQuery(sqlclause);

    while (q1.NextRow())
    {
		actionset.SMSTo = q1.GetAsString(0);
		actionset.MailTo = q1.GetAsString(1);
		actionset.SMTPPwd = q1.GetAsString(2);
		actionset.SMTPUsr = q1.GetAsString(3);
		actionset.SMTPSvr = q1.GetAsString(4);
		actionset.SoundFile = q1.GetAsString(5);
		count ++;
    }

	return count;

}
int CDBWrapper::DBClose()
{
	assert(pDatabase != NULL);
	pDatabase->Close();
	delete pDatabase;
	return 1;
}


//Added For DBTool

int CDBWrapper::TableExist( wxString tablename )
{
	if( pDatabase->TableExists(tablename) ) 
		return 1;
	else 
		return -1;
}

int CDBWrapper::GetRecordNumber(wxString strbegin, wxString strend, int tabletype)
{
	enum {TUXEDOSITE, WEBLOGICSITE };
	wxString sqlClause, sqlPart;
	wxSQLite3ResultSet rs;
	wxInt32 nRows = 0;


	//SELECT count(*) from wlsresource where RecTime between '2009-04-30 15:13:17' and '2009-04-30 15:17:42';

	if( tabletype == TUXEDOSITE )
		sqlClause = wxT("SELECT count(*) from tuxresource ");
	else 
		sqlClause = wxT("SELECT count(*) from wlsresource ");

	if( strend == wxT("N/A") )
		sqlPart = wxString::Format( wxT("WHERE RecTime >= '%s';"), strbegin );
	else
		sqlPart = wxString::Format( wxT("WHERE RecTime BETWEEN '%s' and '%s';"), strbegin, strend );

	sqlClause += sqlPart;

	rs = pDatabase->ExecuteQuery( sqlClause );
	while (rs.NextRow())
	{
			nRows = rs.GetInt(0);
	}

	return nRows;
}
wxArrayString CDBWrapper::GetRecordTimeList()
{
	int tableType = 0;
	wxString strRecTime;
	wxArrayString strRecTimeArray;
	wxString sqlClause;
	wxSQLite3ResultSet rs;

	sqlClause = wxT("SELECT RecTime from sysreclist;");
	rs = pDatabase->ExecuteQuery( sqlClause );
	while (rs.NextRow())
	{
			strRecTime = rs.GetAsString(0);
			strRecTimeArray.Add( strRecTime );
	}

	return strRecTimeArray;
}


int CDBWrapper::TuxCltLstQuery(wxString sqlcondition,TuxedoClientArray* tuxcltarray )
{
	TuxedoClient tuxclt;
	wxInt32 nCount = 0;
	wxString sqlClause = wxT("select * from tuxcltlist ");

	if( !pDatabase->TableExists(wxT("tuxcltlist")) ) {
		wxMessageBox("The Database file is incorrect!");
		return 0;
	}

	sqlClause += sqlcondition;

	wxSQLite3ResultSet q1 = pDatabase->ExecuteQuery( sqlClause );

    while (q1.NextRow())
    {
		tuxclt.ConnectTime = q1.GetAsString(0);
		tuxclt.Status = q1.GetAsString(1);
		tuxclt.ClientAddr = q1.GetAsString(2);
		tuxclt.ClientPID = q1.GetAsString(3);
		tuxclt.ClientName = q1.GetAsString(4);
		tuxclt.RecTime = q1.GetAsString(5);
		tuxcltarray->Add(tuxclt);
		nCount ++;
		//cout << (const char*)(q1.GetString(0).mb_str()) << endl;
    }

	return nCount;
}

int CDBWrapper::TuxQueLstQuery(wxString sqlcondition, TuxedoQueueArray* tuxquearray)
{
	TuxedoQueue tuxque;
	wxInt32 nCount = 0;
	wxString sqlClause = wxT("select * from tuxquelist ");

	if( !pDatabase->TableExists(wxT("tuxquelist")) ) {
		wxMessageBox("The Database file is incorrect!");
		return 0;
	}

	sqlClause += sqlcondition;

	wxSQLite3ResultSet q1 = pDatabase->ExecuteQuery( sqlClause );

    while (q1.NextRow())
    {
		tuxque.ProgName = q1.GetAsString(0);
		tuxque.Queued = q1.GetAsString(1);
		tuxque.SvrCnt = q1.GetAsString(2);
		tuxque.QueName = q1.GetAsString(3);
		tuxque.RecTime = q1.GetAsString(4);
		tuxquearray->Add(tuxque);
		nCount ++;
		//cout << (const char*)(q1.GetString(0).mb_str()) << endl;
    }

	return nCount;
}

TuxedoServerArray* CDBWrapper::TuxSvrLstQuery(wxString sqlcondition)
{
	TuxedoServer tuxsvr;
	TuxedoServerArray *tuxsvrarray = new TuxedoServerArray();

	if( !pDatabase->TableExists(wxT("serverlist")) ) {
		wxMessageBox("The Database file is incorrect!");
		return (TuxedoServerArray*)NULL;
	}
	wxSQLite3ResultSet q1 = pDatabase->ExecuteQuery(wxT("select * from serverlist;"));

    while (q1.NextRow())
    {
		tuxsvr.ProgName = q1.GetAsString(0);
		tuxsvr.UseMem = q1.GetAsString(1);
		tuxsvr.CurrService = q1.GetAsString(2);
		tuxsvr.SvrMin = q1.GetAsString(3);
		tuxsvr.SvrMax = q1.GetAsString(4);
		tuxsvr.RqDone = q1.GetAsString(5);
		tuxsvr.ProcessID = q1.GetAsString(6);
		tuxsvr.QueName = q1.GetAsString(7);
		tuxsvr.RecTime = q1.GetAsString(8);
		tuxsvrarray->Add(tuxsvr);
		//cout << (const char*)(q1.GetString(0).mb_str()) << endl;
    }

	return tuxsvrarray;
}
#ifndef INCLUDE_WXOBJARRAY
#define INCLUDE_WXOBJARRAY
#include "wx/arrimpl.cpp"
WX_DEFINE_OBJARRAY(SiteArray);
WX_DEFINE_OBJARRAY(TuxAlarmArray);
WX_DEFINE_OBJARRAY(TuxedoServerArray);
WX_DEFINE_OBJARRAY(TuxedoQueueArray);
WX_DEFINE_OBJARRAY(TuxedoClientArray)

WX_DEFINE_OBJARRAY(WebLogicServerArray);
WX_DEFINE_OBJARRAY(WebLogicJVMArray);
WX_DEFINE_OBJARRAY(WebLogicThreadArray);
WX_DEFINE_OBJARRAY(WebLogicJDBCArray);

WX_DEFINE_OBJARRAY(TuxResourceArray);
WX_DEFINE_OBJARRAY(WlsResourceArray);
WX_DEFINE_OBJARRAY(SysResourceArray);

#endif
